package com.yuexun.dhdoctor.module.personalPlan.ui;

import android.view.View;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.personalPlan.bean.PlanBean;
import com.yuexun.dhdoctor.module.personalPlan.model.impl.OtherPlanModel;
import com.yuexun.dhdoctor.module.personalPlan.presenter.impl.OtherPlanPresenter;
import com.yuexun.dhdoctor.module.personalPlan.ui.view.IOtherPlanView;
import com.yuexun.dhdoctor.util.DateUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2016/9/8.
 */
public class Act_OtherPlan extends BaseKJActivity implements IOtherPlanView {

    int week_count = 0;
    @Bind(R.id.title_tv)
    TextView titleTv;
    @Bind(R.id.tv_week_title)
    TextView tvWeekTitle;
    @Bind(R.id.tv_week_date)
    TextView tvWeekDate;
    @Bind(R.id.task_weektime_mon)
    TextView taskWeektimeMon;
    @Bind(R.id.task_weektime_mon_am)
    TextView taskWeektimeMonAm;
    @Bind(R.id.task_weektime_mon_pm)
    TextView taskWeektimeMonPm;
    @Bind(R.id.task_weektime_tue)
    TextView taskWeektimeTue;
    @Bind(R.id.task_weektime_tue_am)
    TextView taskWeektimeTueAm;
    @Bind(R.id.task_weektime_tue_pm)
    TextView taskWeektimeTuePm;
    @Bind(R.id.task_weektime_wed)
    TextView taskWeektimeWed;
    @Bind(R.id.task_weektime_wed_am)
    TextView taskWeektimeWedAm;
    @Bind(R.id.task_weektime_wed_pm)
    TextView taskWeektimeWedPm;
    @Bind(R.id.task_weektime_thu)
    TextView taskWeektimeThu;
    @Bind(R.id.task_weektime_thu_am)
    TextView taskWeektimeThuAm;
    @Bind(R.id.task_weektime_thu_pm)
    TextView taskWeektimeThuPm;
    @Bind(R.id.task_weektime_fri)
    TextView taskWeektimeFri;
    @Bind(R.id.task_weektime_fri_am)
    TextView taskWeektimeFriAm;
    @Bind(R.id.task_weektime_fri_pm)
    TextView taskWeektimeFriPm;
    @Bind(R.id.task_weektime_sat)
    TextView taskWeektimeSat;
    @Bind(R.id.task_weektime_sat_am)
    TextView taskWeektimeSatAm;
    @Bind(R.id.task_weektime_sat_pm)
    TextView taskWeektimeSatPm;
    @Bind(R.id.task_weektime_sun)
    TextView taskWeektimeSun;
    @Bind(R.id.task_weektime_sun_am)
    TextView taskWeektimeSunAm;
    @Bind(R.id.task_weektime_sun_pm)
    TextView taskWeektimeSunPm;

    String[] dates;
    TextView[] tv_date, tv_date_am, tv_date_pm;
    Map<String, PlanBean> map_plan = new HashMap<String, PlanBean>();//创建一个Map来存放

    OtherPlanPresenter presenter;
    Calendar calendar = Calendar.getInstance();

    @Override
    public void setRootView() {
        setContentView(R.layout.act_otherplan);
        ButterKnife.bind(this);
        tv_date = new TextView[]{taskWeektimeMon, taskWeektimeTue, taskWeektimeWed, taskWeektimeThu, taskWeektimeFri, taskWeektimeSat, taskWeektimeSun};
        tv_date_am = new TextView[]{taskWeektimeMonAm, taskWeektimeTueAm, taskWeektimeWedAm, taskWeektimeThuAm, taskWeektimeFriAm, taskWeektimeSatAm, taskWeektimeSunAm};
        tv_date_pm = new TextView[]{taskWeektimeMonPm, taskWeektimeTuePm, taskWeektimeWedPm, taskWeektimeThuPm, taskWeektimeFriPm, taskWeektimeSatPm, taskWeektimeSunPm};
        dates = new String[7];
    }

    @Override
    public void initData() {
        super.initData();
        presenter = new OtherPlanPresenter(new OtherPlanModel(), this);
        calendar.setTime(new Date());
        clear_date();
        set();

        titleTv.setText(getIntent().getExtras().getString("name") + "分享的计划");
    }

    private void set() {
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("startDate", DateUtil.getMondayOfThisWeek(calendar));
        params.put("endDate", DateUtil.getSundayOfThisWeek(calendar));
        params.put("shareId", getIntent().getExtras().getString("shareId"));
        presenter.getOtherTaskDetail(params);
        setDateTitle();
        setDate(calendar);
    }

    private void setDateTitle() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月", Locale.getDefault());
        int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
        tvWeekTitle.setText(formatter.format(calendar.getTime()));
        tvWeekDate.setText("第" + weekOfMonth + "周(" +
                DateUtil.getMondayOfThisWeek_Title(calendar) + "-" + DateUtil.getSundayOfThisWeek_Title(calendar) + ")");

    }

    private void setDate(Calendar calendar) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(calendar.getTime());
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd", Locale.getDefault());
        calendar1 = DateUtil.getMondayOfThisWeek_Calendar(calendar1);
        for (int i = 0; i < 7; i++) {
            dates[i] = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(calendar1.getTime());
            tv_date[i].setText(formatter.format(calendar1.getTime()));
            calendar1.add(Calendar.DATE, 1);
        }
    }


    @Override
    public void showOtherTask(String s) {
        map_plan.clear();
        try {
            JSONObject jsonObject_all = new JSONObject(s);
            JSONArray datas = jsonObject_all.getJSONArray("datas");
            JSONObject data = datas.getJSONObject(0);
            Iterator<?> iterator = data.keys();
            while (iterator.hasNext()) { // 遍历每个key
                String key = (String) iterator.next();

                //切记用optJSONObject();用getJSONObject()在为null的时候会报错哦！！！
                JSONArray ob_array = data.optJSONArray(key);
                JSONObject ob = ob_array.getJSONObject(0);
                PlanBean planBean = null;
                if (ob != null) {   //记得判断哦！！
                    planBean = new PlanBean();
                    planBean.setAmpm(ob.optString("ampm"));
                    planBean.setContent(ob.optString("content"));
                    planBean.setPersonalId(ob.optInt("personalId"));
                    planBean.setPersonalName(ob.optString("personalName"));
                    planBean.setStartDatetime(ob.optString("startDatetime"));
                    planBean.setStateDescrption(ob.optString("stateDescrption"));
                    planBean.setState(ob.optInt("state"));
                    planBean.setStartDate(ob.optString("startDate"));
                    planBean.setPersonalScheduleId(ob.optString("personalScheduleId"));
                    map_plan.put(key, planBean);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        setPlan(map_plan);
    }

    private void setPlan(Map<String, PlanBean> map) {
        if (map.size() > 0.) {
            for (String key : map.keySet()) {
                switch (key.substring(0, 1)) {
                    case "1":
                        switch (key.substring(1, 2)) {
                            case "1":
                                tv_date_am[0].setText(map.get(key).getContent());
                                break;
                            case "2":
                                tv_date_pm[0].setText(map.get(key).getContent());
                                break;
                        }
                        break;
                    case "2":
                        switch (key.substring(1, 2)) {
                            case "1":
                                tv_date_am[1].setText(map.get(key).getContent());
                                break;
                            case "2":
                                tv_date_pm[1].setText(map.get(key).getContent());
                                break;
                        }
                        break;
                    case "3":
                        switch (key.substring(1, 2)) {
                            case "1":
                                tv_date_am[2].setText(map.get(key).getContent());
                                break;
                            case "2":
                                tv_date_pm[2].setText(map.get(key).getContent());
                                break;
                        }
                        break;
                    case "4":
                        switch (key.substring(1, 2)) {
                            case "1":
                                tv_date_am[3].setText(map.get(key).getContent());
                                break;
                            case "2":
                                tv_date_pm[3].setText(map.get(key).getContent());
                                break;
                        }
                        break;
                    case "5":
                        switch (key.substring(1, 2)) {
                            case "1":
                                tv_date_am[4].setText(map.get(key).getContent());
                                break;
                            case "2":
                                tv_date_pm[4].setText(map.get(key).getContent());
                                break;
                        }
                        break;
                    case "6":
                        switch (key.substring(1, 2)) {
                            case "1":
                                tv_date_am[5].setText(map.get(key).getContent());
                                break;
                            case "2":
                                tv_date_pm[5].setText(map.get(key).getContent());
                                break;
                        }
                        break;
                    case "7":
                        switch (key.substring(1, 2)) {
                            case "1":
                                tv_date_am[6].setText(map.get(key).getContent());
                                break;
                            case "2":
                                tv_date_pm[6].setText(map.get(key).getContent());
                                break;
                        }
                        break;
                }
            }
        }
    }

    private void clear_date() {
        for (int i = 0; i < 7; i++) {
            tv_date_am[i].setText("");
            tv_date_pm[i].setText("");
        }
    }

    @OnClick({R.id.back,R.id.task_back, R.id.task_now, R.id.task_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.task_back:
                calendar.add(Calendar.DATE, -7);
                week_count--;
                clear_date();
                set();
                break;
            case R.id.task_now:
                calendar.setTime(new Date());
                week_count = 0;
                clear_date();
                set();
                break;
            case R.id.task_next:
                calendar.add(Calendar.DATE, 7);
                week_count++;
                clear_date();
                set();
                break;
        }
    }
}
